﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using EnvDTE80;
using QuickOpen.Framework;

namespace QuickOpen.Controls
{
    public class ItemsListBox : ListBox
    {
        public ItemsListBox() { }

        public void MoveDown() 
        {
            if (Items.Count > 0 && SelectedIndex != Items.Count-1)
                SelectedIndex++;
        }
        
        public void MoveUp() 
        {
            if (Items.Count > 0 && SelectedIndex > 0)
                SelectedIndex--;
        }

        public void ApplyFilter(string filter)
        {
            Items.Clear();
            Items.AddRange(_repository.Query(filter));
            if (Items.Count > 0)
                SelectedIndex = 0;
        }

        public void LoadRepository(IItemRepository repository)
        {
            _repository = repository;
            ApplyFilter(string.Empty);
        }

        public bool PerformAction()
        {
            if (SelectedItem != null)
                return (SelectedItem as IFilterableItem).PerformAction();
            else
                return false;
        }

        private IItemRepository _repository;
    }
}
